package com.example.mvvmrxjavaokhttpretrofit.base;

import android.support.annotation.NonNull;

import com.example.mvvmrxjavaokhttpretrofit.GsonUtil;
import com.example.mvvmrxjavaokhttpretrofit.net.BaseResponse;
import com.example.mvvmrxjavaokhttpretrofit.net.RdSubscriber;
import com.example.mvvmrxjavaokhttpretrofit.net.reflect.TypeBuilder;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

public class BaseModel {

    @Deprecated
    private Observable makeObservable(Observable observable) {
        return observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Deprecated
    protected <T> Observable makeObservableCommon(Observable observable, final Class<T> clazz) {
        return makeObservable(observable.map(new Function<ResponseBody, BaseResponse<T>>() {
            @Override
            public BaseResponse<T> apply(ResponseBody o) throws Exception {
                return GsonUtil.fromJson(o.string(), TypeBuilder
                        .newInstance(BaseResponse.class)
                        .addTypeParam(clazz)
                        .build());
            }
        }));
    }


    /**
     * 待完善
     ****************************************************************************************************************/

    //设置缓存策略
    protected BaseModel setCacheType() {
        return this;
    }

    @NonNull
    @SuppressWarnings("unchecked")
    protected <T> Observable getRemoteDatas(Observable observable, final Class<T> clazz, String requestType) {
        Observable observable1 = observable.map(new Function<ResponseBody, BaseResponse<T>>() {
            @Override
            public BaseResponse<T> apply(ResponseBody responseBody) throws Exception {
                return GsonUtil.fromJson(responseBody.string(), TypeBuilder
                        .newInstance(BaseResponse.class)
                        .addTypeParam(clazz)
                        .build());
            }
        }).doOnNext(new Consumer<BaseResponse<T>>() {
            @Override
            public void accept(BaseResponse<T> baseResponse) throws Exception {
                // TODO: 2019/7/31 0031 可以在这里将数据保存到本地
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());

        observable1.subscribe(new RdSubscriber(requestType) {
            @Override
            protected void onSuccess(int responCode, String responType, Object data) {
                if (iDatasListener != null) {
                    iDatasListener.getSuccess(responCode, responType, data);
                }
            }

            @Override
            protected void onFail(int responCode, String responType, String message, Object data) {
                if (iDatasListener != null) {
                    iDatasListener.getfaild(responCode, responType, message, data);
                }
            }
        });

        return observable1;
    }

    protected void callBackLocalDatas(int responCode, String responType, String message, Object data) {
    }

    protected IDatasListener iDatasListener;

    protected BaseModel setiDatasListener(IDatasListener iDatasListener) {
        this.iDatasListener = iDatasListener;
        return this;
    }
}
